﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WFDataBindingExercises
{
    public partial class EditForm : Form
    {
        public string TownName { get; set; }
        public int Population { get; set; }

        public EditForm()
        {
            InitializeComponent();
        }

        public EditForm(string townName, int townPopulation)
        {
            InitializeComponent();
            this.townGroupBox.Text = "Edit town - " + townName;
            this.nameTextBox.Text = townName;
            this.populationTextBox.Text = townPopulation.ToString();
        }

        private void nameTextBox_Validating(object sender, CancelEventArgs e)
        {
            if (this.nameTextBox.Text.Length < 2)
            {
                this.errorProvider.SetError(nameTextBox, "Name should be longer");
            }
            else if (char.IsLower(nameTextBox.Text[0]))
            {
                this.errorProvider.SetError(nameTextBox, "Name should start with capital letter");
            }
            else
            {
                this.errorProvider.SetError(nameTextBox, "");
            }
        }

        private void populationTextBox_Validating(object sender, CancelEventArgs e)
        {
            foreach (char c in populationTextBox.Text)
            {
                if (!char.IsNumber(c))
                {
                    errorProvider.SetError(populationTextBox, "Only numbers are allowed in this field");
                    return;
                }
                else
                {
                    errorProvider.SetError(populationTextBox, "");
                }
            }
            int pop = 0;
            int.TryParse(this.populationTextBox.Text, out pop);
            if (pop < 0)
            {
                errorProvider.SetError(populationTextBox, "Population should be at least 1");
            }
            else
            {
                errorProvider.SetError(populationTextBox, "");
            }
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void editButton_Click(object sender, EventArgs e)
        {
            if (errorProvider.GetError(nameTextBox) == "" && errorProvider.GetError(populationTextBox) == "")
            {
                DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show("Invalid values. No changes were made.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void nameTextBox_Leave(object sender, EventArgs e)
        {
            if (errorProvider.GetError(nameTextBox) == "")
            {
                TownName = nameTextBox.Text;
            }
        }

        private void populationTextBox_Leave(object sender, EventArgs e)
        {
            if (errorProvider.GetError(populationTextBox) == "")
            {
                Population = int.Parse(populationTextBox.Text);
            }
        }
    }
}
